/**
 * Created by yan on 16/2/24.
 */
var flowStatus = 0;   //标识文件运行状态 0:未导入文件状态 1:文件已导入,未解析入库 2:文件已入库解析完成
var templateCode = "DEFAULT_TEMPLATE";    //对应父页面文件入业务表模板
var folderName = "default";   //对应父页面上传文件存储文件夹名称
var respUpload = new Object();
var commonImportFileComplate = undefined;
var parentIframe = undefined;

$(function () {
    /**
     * 初始化参数
     */
    if (parent != self) {

        if (parent.flowStatus != undefined) {
            self.flowStatus = parent.flowStatus;
        }

        if (parent.templateCode != undefined) {
            self.templateCode = parent.templateCode;
        }

        if (parent.folderName != undefined) {
            self.folderName = parent.folderName;
        }

        if (parent.commonImportFileComplate != undefined && typeof parent.commonImportFileComplate === "function") {
            self.commonImportFileComplate = parent.commonImportFileComplate;
        }

    }
    ;

});

function getParentIframe() {
    var pfs = parent.frames;
    for (var i = 0; i < pfs.length; i++) {
        if (pfs[i] == self) {
            return pfs[i];
        }
    }
}

/************************************业务方法**********************************************/
//初始化数据
function initData() {

    if (self.flowStatus == 0 || self.flowStatus == 2) {
        if (self.templateCode && self.folderName) {
            importFile();
        } else {
            top.$.messager.alert('错误信息', "页面错误", 'error');
            return;
        }
    } else {
        top.$.messager.alert('提示信息', "请选择导入文件", 'info');
        return;
    }
}


//导入校验
function importFile() {
    var filename = $("#flowFile").val();//获取文件名格式为："C:\fakepath\git开发过程.png"
    if ("" == filename.trim()) {
        top.$.messager.alert("提示", "请先上传文件", "info");
        return;
    }
    //判断导入文件类型是否支持
    var type = filename.substring(filename.lastIndexOf(".") + 1).toLocaleLowerCase();
    if ("xls" != type && "xlsx" != type && "txt" != type) {
        top.$.messager.alert('错误信息', "导入文件类型或文件内容格式不支持!", 'error');
        return;
    }
    top.$.messager.confirm("确认", "确定要导入所选文件吗？", function (confirmFlag) {//点击取消时传入false，确定传入true
        if (confirmFlag) {
            //文件FTP上传(fileId\DoneCode由后台FTP部分生成)
            top.$.messager.progress({text: "文件上传中......"});//开启导入中进度条
            fileUploadToFTP();

            ////重置iframe
            //window.parent["commonParseInsert"].contentDocument.location.reload(true);
            respUpload.refresh = true;
        }
    });
}

function callbackParent(callback) {
    if (callback == undefined) {
        return;
    }
    ;
    if (typeof callback === "funciton") {
        callback();
    }
    ;
};

//文件FTP上传
function fileUploadToFTP() {
    //AjaxFileUpload插件会自动生成一个form提交表单。(可能跟ajax无法提交直接导出excel，需要隐藏form提交道理差不多)
    $.ajaxFileUpload({
        url: '/dealFile/uploadFileToFTP?folderName=' + folderName,              //上传处理程序地址
        secureuri: false,                                //是否启用安全提交，默认为false
        fileElementId: 'flowFile',                      //需要上传的文件域的ID，即<input type="file">的ID
        dataType: 'json',                                //服务器返回的数据类型。可以为xml,script,json,html。如果不填写，jQuery会自动判断
        type: 'get',                                      //当要提交自定义参数时，这个参数要设置成post
        success: function (data, status) {
            if (data.success) {
                //记录bpFile
                recordBpFile(data);

                /**
                 * 拼装返回值
                 */
                respUpload.data = data;
                respUpload.flowStatus = 1;
                self.flowStatus = 1;
                //修改父页面参数值
                //window.parent.uploadFileData = data;
                //window.parent.flowStatus = 1;//标示文件已上传成功
                //callbackParent(self.$("#callback"));
                self.commonImportFileComplate(respUpload);

                top.$.messager.alert("提示信息", data.message, "info");
                top.$.messager.progress('close');
            } else {
                top.$.messager.progress('close');
                top.$.messager.alert("错误信息", data.message, "error");
            }
        },
        error: function (data, status, e) {
            top.$.messager.progress('close');	// hide progress bar while submit successfully
            top.$.messager.alert('错误信息', "请求发生错误", 'error');
        }
    });
}

//文件上传ftp成功后,记录bpFile表信息
function recordBpFile(recordData) {
    recordData.templateCode = templateCode;
    $.ajax({
        type: 'POST',
        url: "/dealFile/recordBpFile",
        data: recordData,
        dataType: "json",
        async: false,
        success: function (result) {
            if (!result.success) {
                top.$.messager.progress('close');
                top.$.messager.alert('错误信息', result.message, 'error');
            }
        },
        error: function (result) {
            top.$.messager.progress('close');	// hide progress bar while submit successfully
            top.$.messager.alert('错误信息', "请求发生错误", 'error');
        }
    });
}

